<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="renderer" content="webkit">
    <base href="${rc.contextPath}/">
    <title>创建活动</title>
    <meta name="keywords" content="创建活动">
    <meta name="description" content="创建活动">


    <#include "/commons/stylesheets.ftl">

    <link href="/css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="/css/plugins/datapicker/datepicker3.css" rel="stylesheet">
    <link href="/layer/skin/layer.css" rel="stylesheet">
</head>

<body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">
            <div class="row">
                <div class="col-sm-12">
                    <div class="ibox float-e-margins">

                        <div class="ibox-content">
                            <form class="form-horizontal m-t" id="signupForm" action="activity/saveActivity" method="post" >
                               <input type="hidden" id="selectCompanyIds" name="selectCompanyIds"  />
                               <input type="hidden" id="selectedCompanyIds"  value="${activity.companyIds!''}"/>
                               <input type="hidden"  id="activityId" name="id" value="${activity.id!''}"/>
                             <div class="form-group">
                                       <label class="col-sm-2 control-label">活动企业
                                       </label>

                                        <div class="col-sm-5">
                                            <#if (activity.id) ??>
                                                <a href="javaScript:openDialog('company/querySelectedCompany?activityId=${activity.id}','已选择企业列表',900,420);">已选择<span id="selectCompanyNum" >

                                                <#if (activity.companyIds?? && activity.companyIds?length >0) >
                                                     ${activity.companyIds?split(",")?size}

                                                <#else> 0
                                                </#if>
                                                </span>家企业参与活动</a>
                                             <#else>
                                                <a href="javaScript:openDialog('company/querySelectCompany?selectCompanyIds=' + $('#selectedCompanyIds').val() ,'已选择企业列表',900,420);">已选择<span id="selectCompanyNum" >0</span>家企业参与活动</a>

                                             </#if>
                                            <button type="button" class="btn btn-success" onClick="openDialog('company/queryCompany?isShowAddButton=1&activityId=${activity.id!''}&selectCompanyIds=' + $('#selectedCompanyIds').val() ,'添加企业',1000,520);">添加更多企业</button>
                                       </div>
                              </div>
                               <div class="hr-line-dashed"></div>

                               <div class="form-group">
                                    <label class="col-sm-2 control-label">活动项目</label>

                                    <div class="col-sm-3">
                                        <label class="checkbox-inline">
                                            <input type="checkbox" name="activityType"  <#if activity.activityType?? && activity.activityType?index_of("1")!=-1>checked="checked"</#if> value="1">服务</label>
                                        <label class="checkbox-inline">
                                            <input type="checkbox"  name="activityType"  <#if activity.activityType?? && activity.activityType?index_of("2")!=-1>checked="checked"</#if>  value="2">产品</label>
                                        <label class="checkbox-inline">
                                            <input type="checkbox"  name="activityType"  <#if activity.activityType?? && activity.activityType?index_of("3")!=-1>checked="checked"</#if>  value="3">礼品</label>
                                    </div>
                                </div>


                                <div class="form-group">
                                    <label class="col-sm-2 control-label">具体描述
                                    </label>

                                    <div class="col-sm-5">
                                         <textarea class="form-control" id="activityDesc" name="activityDesc"   rows="2">${activity.activityDesc!''}</textarea>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class="col-sm-2 control-label">活动名称</label>

                                    <div class="col-sm-3">
                                        <input type="text" class="form-control" id="activityName" name="activityName" value="${activity.activityName!''}" aria-required="true" aria-invalid="false" class="valid" >
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class="col-sm-2 control-label">活动时间</label>

                                    <div class="col-sm-2">
                                        <input type="text" class="form-control layer-date"   timeColumn="true" onclick="WdatePicker({readOnly:true, dateFmt:'yyyy-MM-dd HH:mm:ss', maxDate:'#F{$dp.$D(\'endTime\',{H:-1});}',minDate:'%y-%M-%d {%H+1}'})" id="startTime" name="startTime"  value="${(activity.startTime?string("yyyy-MM-dd HH:mm:ss"))!''}"/>
                                    </div>

                                    <div class="col-sm-2">
                                        <input type="text" class="form-control layer-date"     timeColumn="true" onFocus="WdatePicker({readOnly:true, dateFmt:'yyyy-MM-dd HH:mm:ss', minDate:'#F{$dp.$D(\'startTime\',{H:2});}'})" id="endTime" name="endTime" value="${(activity.endTime?string("yyyy-MM-dd HH:mm:ss"))!''}"/>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class="col-sm-2 control-label">活动地点</label>

                                    <div class="col-sm-5">
                                        <input type="text" class="form-control" id="activityLocation" name="activityLocation"  value="${activity.activityLocation!''}" aria-required="true" aria-invalid="false" class="valid" >
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class="col-sm-2 control-label">活动区域</label>

                                    <div class="col-sm-2">
                                        <select class="form-control" id="activityArea" name="activityArea" value="${activity.activityArea!''}">
                                            <option value=""></option>
                                        </select>
                                    </div>
                                </div>


                                <div class="form-group">
                                    <label class="col-sm-2 control-label">是否限量</label>

                                    <div class="col-sm-10">

                                        <div class="col-sm-2">
                                        <label>
                                        <input type="checkbox" id="isLimited" name="isLimited" value="1"   <#if "${activity.isLimited!''}"=="1">checked="checked"</#if> class="i-checks">需要限量</label>
                                        </div>

                                        <div class="col-sm-4" >
                                            <label class="control-label col-sm-4">预发数量</label>
                                            <input type="text" class="col-sm-3" <#if "${activity.isLimited!''}"=="1"> <#else>disabled="true" </#if>  id="limitNumber" name="limitNumber" value="${activity.limitNumber!''}">
                                            <label class="control-label col-sm-1">份</label>
                                            </div>

                                    </div>
                                </div>


                                <div class="form-group">
                                    <label class="col-sm-2 control-label">参与链接</label>

                                    <div class="col-sm-5">
                                        <input type="text" class="form-control" id="activityUrl" name="activityUrl" value="${activity.activityUrl!''}" aria-required="true" aria-invalid="false" class="valid" >
                                    </div>
                                </div>

                                 <div class="form-group" >
                                    <label class="col-sm-2 control-label">发送时间
                                    </label>

                                    <div class="col-sm-8 checkbox-inline " >
                                    	<div class="col-sm-4">
                                            <label class="checkbox-inline i-checks">
                                                <input type="radio"  class="radioItem" checked="" value="1" name="smsSendType" <#if "${activity.smsSendType!'1'}"=="1">checked</#if>  >立即发送</label>
                                            <label class="checkbox-inline i-checks">
                                                <input type="radio"  class="radioItem" value="2"  name="smsSendType"   <#if "${activity.smsSendType!'1'}"=="2">checked</#if> >定时发送</label>
                                        </div>
    									<div class="input-group date checkbox-inline col-sm-3" >
                                            <input type="text" class="form-control layer-date"  disabled="true"   timeColumn="true" onFocus="WdatePicker({readOnly:true, dateFmt:'yyyy-MM-dd HH:mm:ss', maxDate:'#F{$dp.$D(\'endTime\',{H:-1});}'})" id="smsSendTime" name="smsSendTime" value="${(activity.smsSendTime?string("yyyy-MM-dd HH:mm:ss"))!''}" >

                                         </div>
                                    </div>
                                </div>


                                <div class="form-group">
                                    <label class="col-sm-2 control-label">验证方式
                                    </label>

                                    <div class="col-sm-5">
                                        <label class="checkbox-inline i-checks">
                                                <input type="radio" checked="" value="1"  name="verifyType" <#if "${activity.verifyType!'1'}"=="1">checked</#if> >二维码</label>
                                        <label class="checkbox-inline i-checks">
                                                <input type="radio" value="2"  name="verifyType" <#if "${activity.verifyType!'1'}"=="2">checked</#if> >数字码</label>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class="col-sm-2 control-label">短信内容
                                    </label>

                                    <div class="col-sm-5">
                                        <#if activity.smsTemplate??>
                                         <textarea class="form-control" id="smsTemplate" name="smsTemplate"  rows="2">${activity.smsTemplate!''}</textarea>
                                        <#else>
                                          <textarea class="form-control" id="smsTemplate" name="smsTemplate"  rows="2">【皇上驾到】</textarea>
                                        </#if>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class="col-sm-2 control-label">通知短信
                                    </label>

                                    <div class="col-sm-5">
                                         <textarea class="form-control" id="attendNotifySmsTemplate" name="attendNotifySmsTemplate"  rows="2">${activity.attendNotifySmsTemplate!''}</textarea>
                                    </div>
                                </div>
                                <div class="hr-line-dashed"></div>

                                <div class="form-group">
                                    <div class="col-sm-4 col-sm-offset-2">
                                        <button class="btn btn-primary" type="submit">确定</button>
                                        <button class="btn btn-white" type="button" onClick="cancel();">取消</button>
                                     </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    <!-- 全局js -->
    <#include "/commons/scripts.ftl">

    <!-- jQuery Validation plugin javascript-->
    <script src="/js/plugins/validate/jquery.validate.min.js"></script>
    <script src="/js/plugins/validate/messages_zh.min.js"></script>

    <script src="/layer/layer.js"></script>
    <script src="/js/app.js"></script>

    <script type="text/javascript" src="/bootstrap-table/bootstrap-table.min.js"></script>
    <script type="text/javascript" src="/bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>
    <!-- WdatePickerr -->
    <script src="/js/plugins/My97DatePicker/WdatePicker.js"></script>
    <script>
        $.validator.setDefaults({
            highlight:function(a){
                $(a).closest(".form-group").removeClass("has-success").addClass("has-error")
            },
            success:function(a){a.closest(".form-group").removeClass("has-error").addClass("has-success")},
            errorElement:"span",
            errorPlacement:function(a,b){
                if(b.is(":radio")||b.is(":checkbox")){
                    a.appendTo(b.parent().parent().parent())
                }else{
                    a.appendTo(b.parent())
                }
            },
            errorClass:"help-block m-b-none",
            validClass:"help-block m-b-none"
        });

        $().ready(function(){
            $("#commentForm").validate();
            var a="<i class='fa fa-times-circle'></i> ";
            $("#signupForm").validate({
                rules:{
                    activityName:{required:true,maxlength:20},
                    activityLocation:{required:true,maxlength:50},
                    activityArea:"required",
                    activityUrl:{required:true,url:true},
                    smsSendType:"required",
                    verifyType:"required",
                    smsTemplate:"required",
                    attendNotifySmsTemplate:"required",
                    activityType:"required",
                    activityDesc:{required:true,maxlength:100},
                    startTime:"required",
                    endTime:{required:true,compareStartDate:"#startTime"},
                    limitNumber:{digits:true,positiveInteger:true}
                },
                messages:{
                    activityName:{required:a+"请输入活动名称",maxlength:a+"活动名称限制20字"},
                    activityLocation:{required:a+"请输入活动地点",maxlength:a+"活动地点限制50字"},
                    activityDesc:{required:a+"请输入活动描述",maxlength:a+"活动描述限制100字"},
                    endTime:{required:a+"结束时间不能为空",compareStartDate:a+"活动结束时间必须大于开始时间"},
                    smsSendTime:{compareEndDate:a+"短信发送时间必须小于活动结束时间"},
                    limitNumber:{digits:"必须输入整数",positiveInteger: "清输入正整数"}
                },
                success : function(label) {
                    label.closest('.form-group').removeClass('has-error');
                    label.remove();
                },
                errorPlacement : function(error, element) {
                    element.parent('div').append(error);
                },
                submitHandler : function(form) {
                    if($("#selectedCompanyIds").val() == '') {
                        layer.msg('请选择企业');
                        return false;
                    }
                    form.submit();
                }
            });

            jQuery.validator.addMethod("positiveInteger", function(value, element) {
                var aint=parseInt(value);
                return aint>0&& (aint+"")==value;
            }, "清输入正整数");

            //活动结束时间要大于开始时间
            jQuery.validator.methods.compareStartDate = function(value, element, param) {
                var startDate = jQuery(param).val();

                var startTime = new Date(startDate.replace(/-/g,"/"));
                var endTime = new Date(value.replace(/-/g,"/"));
                return startTime < endTime;
            };

            //短信发送时间要小于结束时间
            jQuery.validator.methods.compareEndDate = function(value, element, param) {
                var endDate = jQuery(param).val();

                var endTime = new Date(endDate.replace(/-/g,"/"));
                var sendTime = new Date(value.replace(/-/g,"/"));
                return sendTime < endTime;
            };

            $("#isLimited").change( function() {
                var type = $("#isLimited").val();
                if($("#isLimited").is(':checked')){
                    $('#limitNumber').attr("disabled",false);
                    $("#limitNumber").rules("remove");
                    $("#limitNumber").rules("add", { required: true,digits: true,positiveInteger:true });
                } else {
                    $('#limitNumber').val("");
                    $('#limitNumber').attr("disabled",true);
                    $("#limitNumber").rules("remove");
                    $("#limitNumber").closest('.form-group').removeClass('has-error');

                }
             });

            $(".radioItem").change( function() {
                var val=$('input:radio[name="smsSendType"]:checked').val();
                 if(val == "2"){
                     $("#smsSendTime").rules("remove");
                     $("#smsSendTime").rules("add", { required: true,compareEndDate:"#endTime"});
                     $('#smsSendTime').attr("disabled",false);
                 } else {
                     $('#smsSendTime').val("");
                     $("#smsSendTime").rules("remove");
                     $("#smsSendTime").closest('.form-group').removeClass('has-error');
                     $('#smsSendTime').attr("disabled",true);
                 }
              });

        });


        //数组去重
        Array.prototype.unique = function()
        {
            var n = {},r=[]; //n为hash表，r为临时数组
            for(var i = 0; i < this.length; i++) //遍历当前数组
            {
                if (!n[this[i]]) //如果hash表中没有当前项
                {
                    n[this[i]] = true; //存入hash表
                    r.push(this[i]); //把当前数组的当前项push到临时数组里面
                }
            }
            return r;
        }

        //添加企业，刷新页面
        function refreshCompany(companyIds){
            var selectedCompanyIds = $("#selectedCompanyIds").val();
            var curCompanyArr= new Array();
            curCompanyArr = selectedCompanyIds.split(',');
            if(selectedCompanyIds == "") {
                curCompanyArr= new Array();
            }
            var newArr=curCompanyArr.concat(companyIds);

            newArr = newArr.unique();
            $("#selectedCompanyIds").val(newArr);
            //新建活动时，需要添加的企业
            if($("#activityId").val() == ""){
                $("#selectCompanyIds").val(newArr);
            }

            $("#selectCompanyNum").html(newArr.length);
        }

        //解绑企业，刷新页面
        function delCompany(companyIds){
            var selectedCompanyIds = $("#selectedCompanyIds").val();
            var curCompanyArr= new Array();
            curCompanyArr = selectedCompanyIds.split(',');
            if(selectedCompanyIds == "") {;
                curCompanyArr= new Array();
            }
            for(i in companyIds) {
                //console.log(companyIds[i]);
                var cId = companyIds[i] + "";
                var index = $.inArray(cId,curCompanyArr);

                if(index>=0){
                    curCompanyArr.splice(index,1);
                }
            }
            $("#selectedCompanyIds").val(curCompanyArr);
            $("#selectCompanyNum").html(curCompanyArr.length);
        }

         $(function () {
            $.getJSON("company/queryOrganization", function (list) {
                var parent;
                for (var i = 0; i < list.length; i++) {
                    row = list[i];
                    var organizationId = row.organizationId;
                    if(organizationId == 0) {
                        continue;
                    }
                    if (row.parentOrganizationId == 0) {
                        parent = $("<optgroup>").attr("label",row.name);
                        $("#activityArea").append(parent);
                    } else {
                        var option = $("<option>").val(row.organizationId).text(row.name);
                        parent.append(option)
                    }
                }
                $("#activityArea").val($("#activityArea").attr("value"));
            });
        });

        function cancel(){
            if($("#activityId").val() == ""){
                 window.parent.document.getElementById('closeCurrentTab').click();
            } else {
                window.history.back(-1);
            }
        }
</script>


</body>

</html>